/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc.config;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import org.j4me.logging.Log;

/**
 *
 * @author vvs
 */
public class Config {
    public Server [] srvList;
    void readSrvList()
    {
        try
        {
            RecordStore rs;
            rs=RecordStore.openRecordStore("servers", true);
            int num=rs.getNumRecords();
            srvList=new Server[num];
            int cn=rs.getNextRecordID()-rs.getNumRecords();
//            Log.warn(String.valueOf(cn));
            for (int i =0;i<num;i++)
            {

                try
                {
                srvList[i]=new Server(rs.getRecord(i+cn));
//                Log.warn(srvList[i].toString());
                }
                catch (Exception e)
                {
                    Log.error("",e);
//                    srvList=null;
                    return;
                }

            }
            rs.closeRecordStore();
        }
        catch(RecordStoreException e)
        {
            Log.error("config: ", e);
        }
    }
    public void saveSrvList()
    {
        try
        {
            RecordStore rs;
            try
            {
               RecordStore.deleteRecordStore("servers");
            }
            catch (Exception e)
            {
                Log.error(null,e);
            }
            rs=RecordStore.openRecordStore("servers", true);

            //for (int i=rs.getNumRecords();i>0;i--)
              //  rs.deleteRecord(i);
            for (int i=0;i<srvList.length;i++)
            {
                byte[] dat=srvList[i].toString().getBytes();
                rs.addRecord(dat, 0,dat.length );

            }
            rs.closeRecordStore();
        }
        catch(RecordStoreException e)
        {
            Log.error(null,e);
            //e.printStackTrace();
        }
    }
    public Server getDefaultSrv()
    {
        for (int i=0;i<srvList.length;i++)
            if (srvList[i].isDef)
                return srvList[i];
        return null;
    }
    public void addServer(Server srv,int index)
    {
        if (srvList!=null)
        {
            Server[] tsl=srvList;
            srvList=new Server[tsl.length+1];
            for (int i=0;i<tsl.length;i++)
                srvList[i]=tsl[i];
            srvList[tsl.length]=srv;
        }
        else
        {
            srvList=new Server[1];
            srvList[0]=srv;
        }
    }
    public void delServer (int index)
    {
        Server[] tsl=srvList;
        srvList=new Server[tsl.length-1];
        for (int i=0;i<index;i++)
            srvList[i]=tsl[i];
        for (int i=index+1;i<tsl.length;i++)
            srvList[i-1]=tsl[i];

    }
    public Server getServer(int index)
    {
        try
        {
            return srvList[index];
        }
        catch(Exception e)
        {
            return null;
        }
    }
    public void setServer(int index,Server srv)
    {
        srvList[index]=srv;
    }
    public int srvCount()
    {
        if (srvList==null)
            return 0;
        return srvList.length;
    }
    public void read()
    {
        readSrvList();
    }
}
